using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using src.TMS.Application.RequestDto;
using TMS.Api.StaticClasses;
using TMS.Application.Common.Interface;
using TMS.Application.RequestDto;
using TMS.Domain.Entity;
namespace src.TMS.Api.Controllers
{
    [ApiController]

    public class DormitoryController : ControllerBase
    {
        private readonly IRepository<Dormitory> _Dormitory;
        private readonly IRepository<Students> _Students;

        public DormitoryController(IRepository<Dormitory> Dormitory, IRepository<Students> Students)
        {
            _Dormitory = Dormitory;
            _Students = Students;
        }

        //添加宿舍
        [HttpPost("/InsertDormitory")]
        public async Task<object> InsertDormitory(InsertDormitoryDto InsertDormitoryDto)
        {
            var d = _Dormitory.Table.ToList();
            var Dormitory = d.FirstOrDefault(x => x.DormName == InsertDormitoryDto.DormName);
            if (Dormitory != null)
            {
                return new
                {
                    code = 400,
                    msg = "该宿舍已存在！",
                    data = Dormitory
                };
            }
            else
            {
                int intA = -1;//这是最后的宿舍人数(可住)
                bool IsNumber = int.TryParse(InsertDormitoryDto.DormCount, out intA);
                // 3、int.TryParse与 int.Parse 又较为类似，但它不会产生异常，转换成功返回 true，转换失败返回 false
                if (IsNumber)
                {
                    //宿舍名不可为空
                    if (InsertDormitoryDto.DormName == "")
                    {
                        return new
                        {
                            code = 400,
                            msg = "宿舍名不可为空",
                            data = ""
                        };
                    }
                    //判断输入的数据必须大于0
                    if (intA > 0)
                    {
                        var NewDormitory = new Dormitory
                        {
                            DormName = InsertDormitoryDto.DormName,
                            DormCount = intA
                        };
                        await _Dormitory.AddAsync(NewDormitory);
                        return new
                        {
                            code = 200,
                            msg = "添加成功",
                            data = ""
                        };
                    }
                    else
                    {
                        return new
                        {
                            code = 400,
                            msg = "宿舍可住人数不可小于0",
                            data = ""
                        };
                    }

                }
                else
                {
                    return new
                    {
                        code = 400,
                        msg = "请正确输入宿舍人数",
                        data = ""
                    };
                }
            }
            // return new { };
        }
        //删除宿舍表中所有数据
        [HttpPost("/DelDormitory")]
        public async Task<object> DelDormitory()
        {
            var s = _Dormitory.Table.ToList();
            await _Dormitory.DeleteBulkAsync(s, true);
            return new { msg = s };
        }

        //获取宿舍表的信息(宿舍号，可住人数已经当前该宿舍以及住了多少人)
        [HttpGet("DormitoryList")]
        public async Task<object> DormitoryList()
        {
            var students = _Students.Table.ToList();
            var Info = _Dormitory.Table.ToList();

            // Info.ForEach((item)=>{});
            object[] DormitoryInfo = new object[Info.Count()];
            for (int i = 0; i < Info.Count(); i++)
            {
                var count = students.Where(x => x.DormId == Info[i].Id).Count();//获取在当前宿舍已入住人数
                DormitoryInfo[i] = new
                {
                    Info[i].Id,//宿舍Id
                    Info[i].DormName,//宿舍名称
                    Info[i].DormCount,//可住人数
                    count

                };
            }
            return new
            {
                code = 200,
                data = DormitoryInfo,
                msg = "获取成功"
            };
        }

        //获取某一个宿舍id里面所有学生的信息
        [HttpGet("DormitoryInfo/{Id}")]
        public async Task<object> DormitoryInfo(Guid Id)
        {
            var students = _Students.Table.ToList();

            //首先要判断是否有该宿舍(其实没有也没问题)
            var dormitories = _Dormitory.Table.ToList();
            var IsExist = dormitories.FirstOrDefault(x => x.Id == Id);
            if (IsExist == null)
            {
                return new
                {
                    code = 400,
                    msg = "不存在该宿舍",
                    data = ""
                };
            }
            else
            {
                var studentsInfo = students.Where(x => x.DormId == Id).ToList();
                return new
                {
                    code = 200,
                    msg = "获取成功",
                    data = studentsInfo
                };
            }

        }

        //往某一个宿舍中添加人数
        [HttpPost("/DormitoryAddStu")]
        public async Task<object> DormitoryAddStu(DormitoryAddStuDto DormitoryAddStuDto)
        {
            var students = _Students.Table.ToList();
            var dormitories = _Dormitory.Table.ToList();

            //首先需要查是否有这个宿舍和这名学生
            var IsExistDormitory = dormitories.FirstOrDefault(x => x.Id == DormitoryAddStuDto.DormId);
            if (IsExistDormitory == null)
            {//宿舍不存在
                return new
                {
                    code = 400,
                    msg = "该宿舍不存在",
                    data = ""
                };
            }
            else
            {//宿舍存在
                var count = students.Where(x => x.DormId == DormitoryAddStuDto.DormId).ToList().Count();

                //进行判断这个宿舍是否已满
                if (IsExistDormitory.DormCount == count)
                {
                    return new
                    {
                        code = 400,
                        msg = "该宿舍人已满",
                        data = ""
                    };
                }
                else
                {
                    //根据学号进行判断是否存在这名学生
                    var StuIsExist = students.FirstOrDefault(x => x.StudentId == DormitoryAddStuDto.StudentId);
                    if (StuIsExist == null)
                    {//不存在该学生
                        return new
                        {
                            code = 400,
                            msg = "该学号不存在",
                            data = ""
                        };

                    }
                    else if (StuIsExist.DormId != new Guid("00000000-0000-0000-0000-000000000000"))
                    {//这名学生已经添加至宿舍(可能在其它宿舍)
                        //返回该学生已在的宿舍
                        var IsD = dormitories.FirstOrDefault(x => x.Id == StuIsExist.DormId);
                        if (IsD != null)//这名学生原来的宿舍是否还存在
                        {//存在
                            return new
                            {
                                code = 400,
                                msg = $"该学生已在{IsD.DormName}宿舍",
                                data = ""
                            };
                        }
                        else
                        {//不存在
                            StuIsExist.DormId = DormitoryAddStuDto.DormId;
                            await _Students.UpdateAsync(StuIsExist);
                            return new
                            {
                                code = 200,
                                msg = "添加成功",
                                data = ""
                            };
                        }
                    }
                    else
                    {//该学生的DormId为00000000-0000-0000-0000-000000000000(就是未分配宿舍)
                        StuIsExist.DormId = DormitoryAddStuDto.DormId;
                        await _Students.UpdateAsync(StuIsExist);
                        return new
                        {
                            code = 200,
                            msg = "添加成功",
                            data = ""
                        };
                    }
                }
            }
            //再查看这个宿舍的人数是否已满
            //未满则添加，已满则返回宿舍人数已满
            // return new { };
        }



        [HttpDelete("DormitoryDelStu/{StudentId}")]
        //删除某一位学生的宿舍id（其实是修改成00000000-0000-0000-0000-000000000000）
        public async Task<object> DormitoryDelStu(string StudentId)
        {
            //获取学号
            var students = _Students.Table.ToList();
            var stuInfo = students.FirstOrDefault(x => x.StudentId == StudentId);//这名学生的信息
            if (stuInfo != null)
            {
                stuInfo.DormId = new Guid("00000000-0000-0000-0000-000000000000");
                await _Students.UpdateAsync(stuInfo);
                return new
                {
                    code=200,
                    msg=$"已移除学号为{stuInfo.StudentId}的宿舍",
                    data=""
                };
            }else{
                return new
                {
                    code=400,
                    msg="该学生不存在",
                    data=""
                };
            }
            // return new { };
        }



    }
}